<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2016/8/18
 * Time: 10:32
 */

namespace Admin\Controller;
use Common\Controller\AdminbaseController;

class MapController extends AdminbaseController
{
    /**
     * 路径计算
     * {String} shippingLat 发货纬度
     * String} shippingLng 发货经度
     * {String} collectLat 收货纬度
     * String} collectLng 收货经度
     * order_id 订单id
     */
    function maproute(){
       $ret = M('order')->where(array('order_id'=>I('order_id')))->field('status')->find();
        $shippingLat = (double)I('shippingLat');
        $shippingLng = (double)I('shippingLng');
        $collectLat = (double)I('collectLat');
        $collectLng = (double)I('collectLng');
        $centerLat = ($shippingLat + $collectLat)/2;
        $centerlng = ($shippingLng + $collectLng)/2;
        if($ret){
            $data = array(
                'shippingLat'=>$shippingLat,
                'shippingLng'=>$shippingLng,
                'centerLat'=>$centerLat,
                'centerlng'=>$centerlng,
                'collectLat'=>$collectLat,
                'collectLng'=>$collectLng,
                'order_id'=>I('order_id'),
                'status'=>$ret['status']
            );
            $this->assign('route',$data);
            $this->display();
        }else{
            return RET_NO_V(400,'订单不存在');
        }

    }
    /**
     * 异步请求跑客位置
     *  order_id 订单id
     */
    function pnRoute(){
        $ret = M('order')->where(array('order_id'=>I('order_id')))->field('dispatch_id')->find();
        if($ret){
            $rets = M('dispatch')->where(array('dispatch_id'=>$ret['dispatch_id']))->field('longitude,latitude')->find();
            if($rets){
                $retss = array(
                    'longitude'=>$rets['longitude'],
                    'latitude' =>$rets['latitude']
                );
                echo json_encode($retss);
            }
        }
    }

    /**
     * 异步请求接单区域5公里内跑客信息
     *  order_id 订单id
     */
    function pnRobRouteCounts(){
        $ret = M('order')->where(array('order_id'=>I('order_id')))->field('order_type,dispatch_id,send_latit,send_longit,reci_latit,reci_longit')->find();
        if($ret){
            $latit=$ret['send_latit'];
            $longit=$ret['send_longit'];
            if($ret['order_type']==2 || $ret['order_type']==3){//代买、排队的经纬度附近的跑客
                $latit=$ret['reci_latit'];
                $longit=$ret['reci_longit'];
            }
            $lon=doubleval($longit);
            $lat=doubleval($latit);

            $squares = returnSquarePoint($lon,$lat);
            $where = " ";

            $Model = M();
            $sql = "select dispatch_id,mobile,longitude,latitude,dispatch_name,place_up_time".
                //",ROUND(6378.138*2*ASIN(SQRT(POW(SIN((22.299439*PI()/180-".$lat."*PI()/180)/2),2)+COS(22.299439*PI()/180)*COS(".$lat."*PI()/180)*POW(SIN((114.173881*PI()/180-".$lon."*PI()/180)/2),2)))*1000) AS juli".
                " from db_dispatch d where  latitude>{$squares['right-bottom']['lat']} and latitude < {$squares['left-top']['lat']} and longitude > {$squares['left-top']['lng']} and longitude < {$squares['right-bottom']['lng']} ".
                " and work_status = 1 and is_sign=2 ";
           //     $where.
              //  " and  (SELECT count(dispatch_id) as count FROM db_order o WHERE o.dispatch_id =d.dispatch_id   and o.status >1 and o.status < 7 and is_reservation=0) = 0".
              //  " ORDER BY ROUND(6378.138*2*ASIN(SQRT(POW(SIN((22.299439*PI()/180-".$lat."*PI()/180)/2),2)+COS(22.299439*PI()/180)*COS(".$lat."*PI()/180)*POW(SIN((114.173881*PI()/180-".$lon."*PI()/180)/2),2)))*1000),praise desc,todaytime desc".
                ;
//    dump($sql);
            $voList = $Model->query($sql);
//    dump($voList[0]['dispatch_id']);
           // dump($voList);
            $pnret['otherpn']=$voList;
            $pnret['pnlongitude']=$longit;
            $pnret['pnlatitude'] =$latit;
            if($pnret){
                echo json_encode($pnret);
            }
        }
    }

    /**
     * 郑州市内跑客信息页面
     *  order_id 订单id
     */
    function allpnroutecounts(){
        $this->display();
    }
    /**
     * 郑州市内用户信息页面
     *  order_id 订单id
     */
    function allptroutecounts(){
        $this->display();
    }
    /**
     * 异步请求郑州市内跑客信息
     *  order_id 订单id
     */
    function allptroute(){

        $user_db = M('app_users u');
        $user_obj = $user_db->field('u.user_id,u.avatar,u.latitude,u.longitude,u.user_name,u.last_login_time')->where('u.latitude is not null ') ->select();
//        dump($user_obj);
        $this->assign('datas',$user_obj);
        if($user_obj){
            echo json_encode($user_obj);
        }

    }
/**
     * 异步请求郑州市内跑客信息
     *  order_id 订单id
     */
     function allpnroute(){

//        $dispatch_db = M('dispatch');
//        $dispatch_obj = $dispatch_db->field('dispatch_id,mobile,longitude,latitude,dispatch_name,place_up_time')->where(array('work_status'=>1,'is_sign'=>2)) ->select();
//
//        if($dispatch_obj){
//            echo json_encode($dispatch_obj);
//        }
        $company = M("company");
        $order_db = M('order o');
        $order_resmsm_db = M('order_resmsm r');
        $Model = M();
        $rets = $company->select();//公司信息

        $ret_data = array();
        $count=0;
        foreach ( $rets as $key => $value){
            //if($key>=1){
            $retd = "select dispatch_id as id,latitude as lat,longitude as lon,"
                    . "dispatch_name,place_up_time,mobile from db_dispatch where "
                    . " work_status =1 and is_sign=2 and company_id=".$value['company_id'] ." and DATE_FORMAT(place_up_time,'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d')";//把已签约正在听单中的跑客根据compan_id分类找出来

            $ret_data['data'][$key]['company_id']=$value['company_id'];
            $ret_data['data'][$key]['company_img']=$value['icon'];
            $ret_data['data'][$key]['data'] = $Model->query($retd);
            
            if($ret_data['data'][$key]['data'] )
            {
                foreach($ret_data['data'][$key]['data'] as $k => $v)
                {
                    $dispatch_id = $v['id'];
                    $where = "dispatch_id = $dispatch_id  &&  pay_status=1 && status>1"; 
                    $ret_data['data'][$key]['data'][$k]['order_num'] = $order_resmsm_db
                                                                     ->where($where)
                                                                     ->count();

                    $ret_data['data'][$key]['data'][$k]['poisition']= $order_resmsm_db->field('order_data')->where($where)
                                                                     ->where($where)
                                                                     ->select();
                    if($ret_data['data'][$key]['data'][$k]['poisition'])
                    {
                         for($j=0;$j<count($ret_data['data'][$key]['data'][$k]['poisition']);$j++)
                            {
                                $order_data= json_decode($ret_data['data'][$key]['data'][$k]['poisition'][$j]["order_data"], true);
                                $ret_data['data'][$key]['data'][$k]['poisition'][$j]['send_longit'] = $order_data['send_longit'];
                                $ret_data['data'][$key]['data'][$k]['poisition'][$j]['send_latit'] = $order_data['send_latit'];
                                $ret_data['data'][$key]['data'][$k]['poisition'][$j]['reci_longit'] = $order_data['reci_longit'];
                                $ret_data['data'][$key]['data'][$k]['poisition'][$j]['reci_latit'] = $order_data['reci_latit'];
                            }
                    }
                }
                $count = $count + count($ret_data['data'][$key]['data']);
            }
        }
        
        $ret_data['count']=$count;

        if($ret_data){
            echo json_encode($ret_data);
        }

    }
}